package transcode

import (
	"context"
	"encoding/json"
	"net/http"
	"time"

	"code.byted.org/bytertc/whiteboard_server/biz/config"
	"code.byted.org/bytertc/whiteboard_server/biz/pkg/http_client"
	"code.byted.org/bytertc/whiteboard_server/biz/service/transcode/task"
)

const (
	wpsWebStartTranscodeTaskPath = "/api/v1/wps/StartTranscodeTask"
)

type callbackInfo struct {
	URL string `json:"url"`
}

type thumbnail struct {
	ThumbnailFormat       int                        `json:"thumbnailFormat"`
	ThumbnailWidth        int                        `json:"thumbnailWidth"`
	ThumbnailHeight       int                        `json:"thumbnailHeight"`
	PreSignDuration       int64                      `json:"preSignDuration"`
	ForceUseResolution    bool                       `json:"forceUseResolution"`
	ExecutorStorageConfig task.ExecutorStorageConfig `json:"executorStorageConfig"`
}

type startTranscodeTaskRequest struct {
	AppID     string       `json:"appId"`
	TaskID    string       `json:"taskId"`
	FileID    string       `json:"fileId"`
	Token     string       `json:"token"`
	Callback  callbackInfo `json:"callback"`
	Thumbnail *thumbnail   `json:"thumbnail"`
}

type startTranscodeTaskResp struct {
	Success bool `json:"success"`
}

func startDynamicTranscodeTask(ctx context.Context, req *startTranscodeTaskRequest) (*startTranscodeTaskResp, error) {
	body, err := http_client.Do(ctx, config.GetConfig().NodeServer+wpsWebStartTranscodeTaskPath, http.MethodPost, nil, nil, req, 5*time.Second)
	if err != nil {
		return nil, err
	}
	resp := &startTranscodeTaskResp{}
	err = json.Unmarshal(body, resp)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
